<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

use app\admin\common\FormHelper;
use app\admin\common\TableHelper;
use app\admin\common\ToolbarHelper;

function sf_html_toolbar_button($text, $icon, $class, $options = array())
{
    return (new ToolbarHelper())->renderButton($text, $icon, $class, $options);
}

function sf_html_table_render($array, $options = array())
{
    return (new TableHelper($array))->renderTable($options);
}

function sf_html_table_header_select($options = array())
{
    return (new TableHelper(null))->renderHeaderSelect($options);
}

function sf_html_table_header_text($text, $options = array())
{
    return (new TableHelper(null))->renderHeaderText($text, $options);
}

function sf_html_table_column_text($row, $field, $options = array())
{
    return (new TableHelper(null))->renderColumnText($row, $field, $options);
}

function sf_html_table_column_select($row, $field, $options = array())
{
    return (new TableHelper(null))->renderColumnSelect($row, $field, $options);
}

function sf_html_table_action($text, $icon, $class, $options = array())
{
    return (new TableHelper(null))->renderAction($text, $icon, $class, $options);
}

function sf_html_form_upload_image($name, $value)
{
    $file_uploader = new \app\admin\common\FileUploader();
    $file_uploader->name = $name;
    $file_uploader->value = $value;
    $file_uploader->upload_type = 'image';
    $file_uploader->file_exts = 'gif,jpg,jpeg,bmp,png';
    $file_uploader->button_text = '上传图片';
    return $file_uploader->display();
}

function sf_html_form_render($array, $errors, $options)
{
    return (new FormHelper($array, $errors))->renderForm($options);
}

function sf_html_form_group($array, $errors, $field, $label, $help, $control, $options = array())
{
    return (new FormHelper($array, $errors))->renderGroup($field, $label, $help, $control, $options);
}

function sf_html_form_control($array, $errors, $field, $label, $help, $control, $options = array())
{
    return (new FormHelper($array, $errors))->renderControl($field, $label, $help, $control, $options);
}

function sf_html_form_text($array, $errors, $field, $options = array())
{
    return (new FormHelper($array, $errors))->renderText($field, $options);
}

function sf_html_form_switch($array, $errors, $field, $options = array())
{
    return (new FormHelper($array, $errors))->renderSwitch($field, $options);
}

function sf_html_form_label($label)
{
    return (new FormHelper(null, null))->renderLabel($label);
}

function sf_html_form_help($help)
{
    return (new FormHelper(null, null))->renderHelp($help);
}

function sf_html_form_error($error)
{
    return (new FormHelper(null, null))->renderError($error);
}

function sf_html_form_button($text, $icon, $class, $options = array())
{
    return (new FormHelper(null, null))->renderButton($text, $icon, $class, $options);
}

function sf_html_form_submit_and_cancel($submit_url, $cancel_url)
{
    return (new FormHelper(null, null))->renderSubmitAndCancel($submit_url, $cancel_url);
}

function sf_format_date($val)
{
    return date('Y-m-d', $val);
}

function sf_php_dateformat_to_jQueryUI($php_format)
{
    $SYMBOLS_MATCHING = array(
        // Day
        'd' => 'dd',
        'D' => 'D',
        'j' => 'd',
        'l' => 'DD',
        'N' => '',
        'S' => '',
        'w' => '',
        'z' => 'o',
        // Week
        'W' => '',
        // Month
        'F' => 'MM',
        'm' => 'mm',
        'M' => 'M',
        'n' => 'm',
        't' => '',
        // Year
        'L' => '',
        'o' => '',
        'Y' => 'yy',
        'y' => 'y',
        // Time
        'a' => '',
        'A' => '',
        'B' => '',
        'g' => '',
        'G' => '',
        'h' => '',
        'H' => '',
        'i' => '',
        's' => '',
        'u' => ''
    );
    $jqueryui_format = "";
    $escaping = false;
    for ($i = 0; $i < strlen($php_format); $i++) {
        $char = $php_format[$i];
        if($char === '\\') {
            // PHP date format escaping character
            $i++;
            if($escaping) $jqueryui_format .= $php_format[$i];
            else $jqueryui_format .= '\'' . $php_format[$i];
            $escaping = true;
        } else {
            if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
            if(isset($SYMBOLS_MATCHING[$char]))
                $jqueryui_format .= $SYMBOLS_MATCHING[$char];
                else
                    $jqueryui_format .= $char;
        }
    }
    return $jqueryui_format;
}